package handlers

import (
	"car-trade-sys/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Add(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	roleNode := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := model.DB.Create(&roleNode).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "关联失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
	})
	return
}

func RList(c *gin.Context) {
	rid := c.Query("rid")
	var node []model.Node
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		WHERE
		  nodes.id = 1
	*/
	model.DB.Table("nodes").Joins(" LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").Where("role_nodes.rid = ?", rid).Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"data": node,
		"msg":  "success",
	})
}
